<template>
	<uv-popup ref="popupBox" mode="bottom" :round="10" @maskClick="closeFunc">
		<view class="title">上传勘场资料</view>
		<scroll-view class="scroll-view" scroll-y>
			<view class="uv-form-box">
				<uv-form labelPosition="top" :model="formData" :rules="rules" ref="form" labelWidth="auto">
					<uv-form-item label="资料文件(.zip,.ppt,.xls,.pdf,.doc,.docx)" prop="survey_data" labelPosition="left"
						required>
						<view class="file-box">
							<uv-button type="primary" text="点击上传" @click="uploadFunc('survey_data')"
								customTextStyle="font-weight: 400;font-size: 24rpx;color: #FFFFFF;"
								customStyle="width: 170rpx;height: 60rpx;background: #3B75FF;border-radius: 16rpx;"></uv-button>
						</view>
					</uv-form-item>
					<view class="file-list">
						<view class="file-item" v-for="(item, index) in formData.survey_data" :key="index">
							<view class="file-name">{{ item.name }}</view>
							<uv-icon name="close" color="#A4A4A4" @click="delFile(index, 'survey_data')"></uv-icon>
						</view>
					</view>
					<uv-form-item label="备注" prop="remark">
						<uv-textarea height="100" v-model="formData.remark" placeholder="请输入内容"></uv-textarea>
					</uv-form-item>
				</uv-form>
			</view>
		</scroll-view>
		<view class="btn-box">
			<uv-button @click="closeFunc" text="取 消" customTextStyle="font-weight: 400;font-size: 28rpx;color: #8F8F8F;"
				customStyle="width: 180rpx;height: 80rpx;background: #FFFFFF;border-radius: 16rpx;border: 2rpx solid #E3E3E3;margin-bottom:20rpx;"></uv-button>
			<uv-button @click="formSubmit" type="primary" text="提 交"
				customTextStyle="font-weight: 400;font-size: 28rpx;color: #FFFFFF;"
				customStyle="width: 500rpx;height: 80rpx;background: #3B75FF;border-radius: 16rpx;margin-bottom:20rpx;"></uv-button>
		</view>
	</uv-popup>
</template>

<script setup>
	import {
		computed,
		watch,
		onMounted,
		getCurrentInstance,
		ref,
		defineEmits
	} from 'vue';
	const {
		proxy: that
	} = getCurrentInstance();
	let props = defineProps({
		show: {
			type: Boolean,
			default: () => {
				return false;
			}
		},
		dataObj: {
			type: Object,
			default: () => {
				return {};
			}
		}
	});
	// 表单数据
	let formData = ref({
		survey_data: [],
		remark: ''
	});
	// 表单规则
	let rules = ref({
		survey_data: [{
			asyncValidator: (rule, value, callback) => {
				if (!value.length) {
					callback(new Error('请上传资料文件'));
				} else {
					// 如果校验通过，也要执行callback()回调
					callback();
				}
			}
		}]
	});

	onMounted(() => {
		// that.$refs.popupBox.open();
		// getCompanyList();
	});

	watch(
		() => props.show,
		(newValue, oldValue) => {
			console.log(newValue, oldValue, '值变化');
			if (props.show) {
				that.$refs.popupBox.open();
			} else {
				formData.value = {
					survey_data: [],
					remark: ''
				};
				that.$refs.form.clearValidate();
				that.$refs.popupBox.close();
			}
		}
	);
	const emit = defineEmits(['close', 'onSuccess']);

	const closeFunc = () => {
		emit('close', false);
	};
	// 上传文件
	const uploadFunc = (type) => {
		console.log(type, '类型');
		switch (type) {
			case 'survey_data':
				uni.chooseMessageFile({
					count: 1, //限制选择的文件数量
					type: 'file', //非图片和视频的文件,不选默认为all，file，video，image
					extension: ['zip', 'ppt', 'xls', 'pdf','doc','docx'],//.zip,.ppt,.xls,.pdf,.doc,.docx
					success(res) {
						console.log(res.tempFiles);
						res.tempFiles.map((item) => {
							formData.value[type] = [item];
							that.$refs.form.validateField(type);
						});
					}
				});
				break;
			default:
		}
	};
	// 删除文件
	const delFile = (index, type) => {
		formData.value[type].splice(index, 1);
		that.$refs.form.validateField(type);
	};
	// 表单提交
	const formSubmit = () => {
		let data = formData.value;
		let dataObj = props.dataObj;
		console.log(data, '表单数据', dataObj);
		that.$refs.form
			.validate()
			.then((res) => {
				uni.showLoading({
					mask: true,
					title: '文件上传中...'
				});
				let promiseAll = [];
				if (data.survey_data.length) {
					let file1 = uploadAjax(data.survey_data, 'survey_data');
					promiseAll.push(file1);
				}
				Promise.all(promiseAll)
					.then((result) => {
						uni.hideLoading();
						let obj = {
							remark: data.remark,
							project_id: props.dataObj.id
						};
						console.log(result); //['成功了', 'success']
						result.forEach((item) => {
							obj[item.type] = item.res.arrResult;
						});
						console.log(obj, '参数');
						uni.showLoading({
							mask: true,
							title: '信息提交中...'
						});
						that.$Api
							.wxUploadSurveyData(obj)
							.then((res) => {
								uni.hideLoading();
								if (res.code == 200) {
									emit('onSuccess', true);
								}
								uni.showToast({
									title: res.code == 200 ? '操作成功' : res.message,
									icon: res.code == 200 ? 'success' : 'error'
								});
							})
							.catch((err) => {
								uni.hideLoading();
							});
					})
					.catch((error) => {
						console.log(error);
					});
			})
			.catch((errors) => {});
	};
	const uploadAjax = (fileList, type) => {
		return new Promise((resolve, reject) => {
			that.$upload(fileList).then((res) => {
				console.log(res, '文件上传');
				resolve({
					type,
					res
				});
			});
		});
	};
</script>

<style lang="scss" scoped>
	.title {
		font-size: 32rpx;
		width: 100%;
		line-height: 100rpx;
		text-align: center;
	}

	.btn-box {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 30rpx;
		box-sizing: border-box;
		margin-top: 30rpx;
	}

	.scroll-view {
		width: 100%;
		max-height: 50vh;
		background-color: #fff;

		// padding: 0 30rpx;
		// box-sizing: border-box;
		.uv-form-box {
			padding: 0 30rpx !important;
		}

		.file-box {
			width: 100%;
			display: flex;
			flex-direction: row-reverse;
		}

		.file-list {
			width: 100%;
			margin-bottom: 20rpx;
			display: flex;
			align-items: center;
			flex-wrap: wrap;

			.file-item {
				display: flex;
				justify-content: space-between;
				margin-top: 10rpx;
				margin-left: 10rpx;
				padding: 10rpx;
				background-color: #f8f8f8;
				width: 90%;

				.file-name {
					font-size: 24rpx;
					overflow: hidden;
					/* 确保溢出文本会被隐藏 */
					white-space: nowrap;
					/* 确保文本在一行内显示 */
					text-overflow: ellipsis;
					/* 使用省略号显示文本溢出的部分 */
					width: 80%;
				}
			}

			.file-media {
				width: 150rpx;
				height: 150rpx;
				margin: 0 27rpx 22rpx 0;
				overflow: hidden;
				border-radius: 10rpx;
				position: relative;

				&:nth-child(4n) {
					margin-right: 0;
				}

				.media-item {
					width: 100%;
					height: 100%;
				}

				.icon-box {
					position: absolute;
					z-index: 1;
					top: 50%;
					left: 50%;
					transform: translate(-50%, -50%);
					background-color: rgba(0, 0, 0, 0.6);
					width: 70rpx;
					height: 70rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					border-radius: 50%;
				}

				.del-icon {
					width: 60rpx;
					height: 40rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					position: absolute;
					right: 10rpx;
					top: 10rpx;
					z-index: 1;
					background-color: rgba(0, 0, 0, 0.6);
					border-radius: 10rpx;
				}
			}
		}
	}
</style>